package com.qyhl.webtv.module_microvideo.shortvideo.classic.detail.comment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.entity.microvideo.ClassicCommentBean;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ClassicCommentFragment extends BaseFragment implements ClassicCommentContract.ClassicCommentView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    private View view;

    private List<ClassicCommentBean> mDataList = new ArrayList<>();

    private CommonAdapter<ClassicCommentBean> commonAdapter;

    private int page = 1;

    private ClassicCommentPresenter mPresenter;

    private String programId;

    private String title;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setProgramId(String programId) {
        this.programId = programId;
    }

    public static ClassicCommentFragment newInstance(String title, String programId) {
        ClassicCommentFragment fragment = new ClassicCommentFragment();
        fragment.setProgramId(programId);
        fragment.setTitle(title);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.micro_fragment_classic_comment, null);
            ButterKnife.bind(this, view);

        }
        return view;
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new ClassicCommentPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<ClassicCommentBean>(getContext(), R.layout.micro_item_classic_comment, mDataList) {
            @Override
            protected void convert(ViewHolder holder, ClassicCommentBean bean, int position) {
                holder.setText(R.id.ask_name, bean.getUserName());

                holder.setText(R.id.ask_date, DateUtils.mTranslateDate(bean.getCreateTime()));

                holder.setText(R.id.question, bean.getQuestion());

                if (bean.getAnswer() != null && bean.getAnswer().getCreateTime() != null) {
                    holder.setVisible(R.id.answer, true);
                    holder.setVisible(R.id.answer_date, true);
                    holder.setVisible(R.id.tips, true);

                    holder.setText(R.id.answer_date, DateUtils.mTranslateDate(bean.getAnswer().getCreateTime()));

                    holder.setText(R.id.answer, bean.getAnswer().getAnswer());

                } else {
                    holder.setVisible(R.id.tips, false);
                    holder.setVisible(R.id.answer, false);
                    holder.setVisible(R.id.answer_date, false);
                }
            }
        });

        mPresenter.getList(programId, page + "");
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                mPresenter.getList(programId, page + "");
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getList(programId, page + "");
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(programId, page + "");
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setList(List<ClassicCommentBean> beanList, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        page++;
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(getContext())) {
                showToast("网络异常，请检查您的网络！", 4);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            if (!NetUtil.isNetworkAvalible(getContext())) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }
}
